package com.wondertek.poms.service.controller;

import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.common.exception.MessageCodes;
import com.wondertek.poms.common.utils.BeanPropertiesUtil;
import com.wondertek.poms.common.utils.CopyUtils;
import com.wondertek.poms.common.utils.validate.ValidateFilter;
import com.wondertek.poms.common.vo.PageBean;
import com.wondertek.poms.common.vo.ResultBean;
import com.wondertek.poms.core.service.IAdvanceModelSaleService;
import com.wondertek.poms.dao.po.AdvanceModelSale;
import com.wondertek.poms.service.vo.AdvanceModelSaleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @author evan0213
 * @description 超前点播模式计费web接口
 */
@Api(value = "AdvanceModelSaleController", description = "超前点播模式计费 接口")
@RestController
@RequestMapping("advance-model-sale")
public class AdvanceModelSaleController {

	@Autowired
	private IAdvanceModelSaleService iAdvanceModelSaleService;
	@Autowired
	private BeanPropertiesUtil beanPropertiesUtil;

	/**
	 * @param advanceModelSaleVo 超前点播模式计费vo
	 * @param page               页数
	 * @param rows               每页的记录数
	 * @param sidx               排序列
	 * @param sord               排序方式
	 * @return 查询的结果
	 */
	@ApiOperation(value = "超前点播模式计费 列表", notes = "超前点播模式计费 列表")
	@GetMapping("/list")
	public ResultBean list(AdvanceModelSaleVo advanceModelSaleVo,
	                       @RequestParam(defaultValue = "1") int page,
	                       @RequestParam(defaultValue = "20") int rows,
	                       @RequestParam(defaultValue = "updatedTime") String sidx,
	                       @RequestParam(defaultValue = "desc") String sord) {
		AdvanceModelSale advanceModelSale = new AdvanceModelSale();
		BeanUtils.copyProperties(advanceModelSaleVo, advanceModelSale);
		Page<AdvanceModelSale> all = iAdvanceModelSaleService.findAll(advanceModelSale, page, rows, sord, sidx);
		return PageBean.ok(all.getTotalPages(), all.getTotalElements(), all.getContent());
	}

	/**
	 * @param id id值
	 * @return 超前点播模式计费
	 */
	@ApiOperation(value = "超前点播模式计费 详情", notes = "超前点播模式计费 详情")
	@GetMapping("/get/{id}")
	public ResultBean get(@PathVariable Long id) {
		AdvanceModelSale advanceModelSale = iAdvanceModelSaleService.findById(id);
		return ResultBean.ok(advanceModelSale);
	}

	/**
	 * 新增超前点播模式计费
	 *
	 * @param advanceModelSaleVo vo对象
	 * @return 新增结果
	 */
	@ApiOperation(value = "超前点播模式计费 新增", notes = "超前点播模式计费 新增")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "advanceTradeName", required = true),
			@ApiImplicitParam(name = "advanceModel", required = true),
			@ApiImplicitParam(name = "advanceTradeStartTime", required = true),
			@ApiImplicitParam(name = "advanceTradeEndTime", required = true),
			@ApiImplicitParam(name = "purchaseLimit", required = true),
			@ApiImplicitParam(name = "advanceProductId", required = true),
			@ApiImplicitParam(name = "warrantType", required = true),
			@ApiImplicitParam(name = "warrantDuration", required = true),
			@ApiImplicitParam(name = "warrantUnit", required = true),
			@ApiImplicitParam(name = "invalidTime", required = true),
	})
	@PostMapping("/add")
	public ResultBean add(AdvanceModelSaleVo advanceModelSaleVo) {
		List<String> messageList = ValidateFilter.getFilterMessage(advanceModelSaleVo, Constants.VALIDATE_DEFAULT);
		if (!messageList.isEmpty()) {
			return ResultBean.error(messageList.toString());
		}

		if (isRepeat(advanceModelSaleVo))
			return ResultBean.error(MessageCodes.get(MessageCodes.M980014));

		AdvanceModelSale advanceModelSale = new AdvanceModelSale();
		BeanUtils.copyProperties(advanceModelSaleVo, advanceModelSale);
		advanceModelSale.setDel(Constants.DELETE_0);
		AdvanceModelSale.putTimeAndUser(advanceModelSale);
		iAdvanceModelSaleService.save(advanceModelSale);
		return ResultBean.ok(advanceModelSale);
	}


	/**
	 * 判断是否重复
	 *
	 * @param advanceModelSaleVo 需要判断对象
	 * @return 重复结果
	 */
	private boolean isRepeat(AdvanceModelSaleVo advanceModelSaleVo) {
		AdvanceModelSale temp = new AdvanceModelSale();
		CopyUtils.copyProperties(advanceModelSaleVo, temp, "id");
		beanPropertiesUtil.setNullIfStringIsBlank(temp);
		temp.setDel(Constants.DELETE_0);
		List<AdvanceModelSale> list = iAdvanceModelSaleService.findAll(Example.of(temp));
		for (AdvanceModelSale rule : list) {
			AdvanceModelSaleVo vo = new AdvanceModelSaleVo();
			AdvanceModelSaleVo tempVo = new AdvanceModelSaleVo();
			CopyUtils.copyProperties(rule, vo, "id");
			CopyUtils.copyProperties(advanceModelSaleVo, tempVo, "id");
			beanPropertiesUtil.setNullIfStringIsBlank(tempVo);
			beanPropertiesUtil.setNullIfStringIsBlank(vo);
			if (vo.equals(tempVo)) {
				if (advanceModelSaleVo.getId() == null || !Objects.equals(advanceModelSaleVo.getId(), rule.getId())) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 超前点播模式计费 删除
	 *
	 * @param ids id拼接字符串，以英文逗号分隔
	 * @return 删除结果
	 */
	@ApiOperation(value = "超前点播模式计费 删除", notes = "超前点播模式计费 删除")
	@ApiImplicitParam(name = "ids", required = true, dataType = "String", value = "超前点播模式计费id拼接字符串，以英文逗号分隔")
	@DeleteMapping(value = "/del")
	@ResponseBody
	public ResultBean delete(String ids) {
		if (StringUtils.isNotBlank(ids)) {
			String[] idsArr = ids.split(",");
			for (String id : idsArr) {
				iAdvanceModelSaleService.deleteLogic(Long.parseLong(id));
			}
		}
		return ResultBean.ok();
	}
}
